package com.company.game00;

import cn.hutool.core.thread.ThreadUtil;
import cn.hutool.core.util.RandomUtil;

import javax.sound.sampled.*;
import java.awt.*;

public class MyUtil {

    public static final int SCREEN_WIDTH = Toolkit.getDefaultToolkit().getScreenSize().width;
    public static final int SCREEN_HEIGHT = Toolkit.getDefaultToolkit().getScreenSize().height;
    // public static final int FRAME_WIDTH = (int) (SCREEN_WIDTH * 0.7);
    // public static final int FRAME_HEIGHT = (int) (SCREEN_HEIGHT * 0.8);
    public static final int FRAME_WIDTH = 960;
    public static final int FRAME_HEIGHT = 720;

    public static final int GAME_STATE_MENU = 0;
    public static final int GAME_STATE_RUN = 1;

    /**
     * 坦克总数
     */
    public static final int ENEMY_MAX_COUNT = 10;
    /**
     * 间隔时间，产生坦克
     */
    public static final int ENEMY_BORN_INTERVAL = 1500;
    /**
     * 间隔时间，坦克随机移动或站立
     */
    public static final int ENEMY_AI_INTERVAL = 1500;
    /**
     * 开火概率
     */
    public static final double ENEMY_FIRE_PERCENT = 0.03;

    public static Color getRandomColor() {
        int red = RandomUtil.randomInt(0, 256);
        int green = RandomUtil.randomInt(0, 256);
        int blue = RandomUtil.randomInt(0, 256);
        return new Color(red, green, blue);
    }

    public static boolean isTouch(int rectX, int rectY, int radius, int pointX, int pointY) {
        if (Math.abs(rectX - pointX) < radius
                && Math.abs(rectY - pointY) < radius) {
            return true;
        }
        return false;
    }

    public static Image getImage(String src) {
        return Toolkit.getDefaultToolkit().getImage(MyUtil.class.getResource(src));
    }

}

class Audio {

    private AudioFormat audioFormat = null;
    private SourceDataLine sourceDataLine = null;
    private DataLine.Info dataLine_info = null;
    private AudioInputStream audioInputStream = null;

    public Audio(String fileName) {
        try {
            audioInputStream = AudioSystem.getAudioInputStream(Audio.class.getResource(fileName));
            audioFormat = audioInputStream.getFormat();
            dataLine_info = new DataLine.Info(SourceDataLine.class, audioFormat);
            sourceDataLine = (SourceDataLine) AudioSystem.getLine(dataLine_info);
            //FloatControl volctrl=(FloatControl)sourceDataLine.getControl(FloatControl.Type.MASTER_GAIN);
            //volctrl.setValue(-40);//
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    byte[] b = new byte[1024 * 1024 * 15];

    public void loop() {
        ThreadUtil.execute(() -> {
            while (true) {
                int len = 0;
                try {
                    sourceDataLine.open(audioFormat, 1024 * 1024 * 15);

                    sourceDataLine.start();
                    //System.out.println(audioInputStream.markSupported());
                    audioInputStream.mark(12358946);
                    while ((len = audioInputStream.read(b)) > 0) {
                        sourceDataLine.write(b, 0, len);
                    }
                    audioInputStream.reset();

                    sourceDataLine.drain();
                    sourceDataLine.close();
                } catch (Exception e) {
                    e.printStackTrace();
                }
            }
        });
    }

    public void play() {
        ThreadUtil.execute(() -> {
            try {
                byte[] b = new byte[1024 * 5];
                int len = 0;
                sourceDataLine.open(audioFormat, 1024 * 5);
                sourceDataLine.start();
                //System.out.println(audioInputStream.markSupported());
                audioInputStream.mark(12358946);
                while ((len = audioInputStream.read(b)) > 0) {
                    sourceDataLine.write(b, 0, len);
                }
                // audioInputStream.reset();
                sourceDataLine.drain();
                sourceDataLine.close();
            } catch (Exception e) {
                e.printStackTrace();
            }
        });
    }

    /*public static void main(String[] args) {
        new Audio("/explode.wav").play();
    }*/
}